package com.csu.letaobackend.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.csu.letaobackend.bean.User;
import com.csu.letaobackend.commom.CommonResponse;
import com.csu.letaobackend.persistence.UserDao;
import com.csu.letaobackend.service.UserService;
import org.apache.ibatis.annotations.Update;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@MapperScan("com.csu.letaobackend.persistence")
public class UserServiceImpl implements UserService {

      @Autowired
      private UserDao userDao;


      @Override
      public CommonResponse<User> getUser(String username) {
            User user = userDao.selectById(username);

            if(user==null){
                  return  CommonResponse.createForError("用户不存在");
            }
            return CommonResponse.createForSuccess(user);
      }


      @Override
      public CommonResponse<User> getUser(String username, String password) {
           QueryWrapper<User> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("username",username);
            queryWrapper.eq("password",password);

            User user = userDao.selectOne(queryWrapper);
            if(user==null){
                  return CommonResponse.createForError("用户名或密码错误");
            }
            return CommonResponse.createForSuccess("登录成功",user);
      }

      @Override
      public CommonResponse<User> getUserByPhone(String memPhone) {
            QueryWrapper<User> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("phone",memPhone);

            User user = userDao.selectOne(queryWrapper);
            if(user==null){
                  return CommonResponse.createForError("该手机号对应账户不存在");
            }
            return CommonResponse.createForSuccess("登录成功",user);
      }

      @Override //测试通过
      public CommonResponse<User> updateUserPassword(String username, String newPassword) {
            User user = userDao.selectById(username);
            if(user==null)
                   return CommonResponse.createForError("用户名不存在");
            else {
                  UpdateWrapper<User> updateWrapper =new UpdateWrapper<>();
                  updateWrapper.eq("username",username);
                  updateWrapper.set("password",newPassword);

                  if(userDao.update(null,updateWrapper)!=0)
                        return CommonResponse.createForSuccessMessage("修改成功");
                  else
                        return  CommonResponse.createForError("修改失败");
            }
      }

      @Override
      public CommonResponse<User> updateUser(User user) {
            User user1 = userDao.selectById(user.getUsername());

            if(user1==null){
                  return  CommonResponse.createForError("用户不存在");
            }
            else{
                  userDao.updateById(user);
            }
            return CommonResponse.createForSuccess("更新成功",user);
      }

      @Override
      public CommonResponse<User> registerUser(User user) {
            String username = user.getUsername();

            QueryWrapper<User> queryWrapper= new  QueryWrapper<>();
            queryWrapper.eq("phone",user.getPhone());

            if (userDao.selectById(username)!=null){
                  return CommonResponse.createForError("用户名已存在");
            }else if(userDao.selectOne(queryWrapper)!=null) {
                  return CommonResponse.createForError("该手机号已注册");
            }
            else {
                  userDao.insert(user);
            }
            return CommonResponse.createForSuccess("注册成功",user);
      }


      @Override
      public CommonResponse<User> deleteUser(String username) {
            User user1 = userDao.selectById(username);

            if(user1==null){
                  return  CommonResponse.createForError("用户不存在");
            }
            else{
                  userDao.deleteById(username);
            }
            return CommonResponse.createForSuccessMessage("删除成功");
      }
}
